package com.yuchen.utils.fastjson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;

import java.io.Serializable;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class JsonUtils {
    private final static SerializeConfig config = new SerializeConfig();

    static{
        config.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd"));
        config.put(Timestamp.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 默认的处理时间
     *
     * @param object
     * @return
     */
    public static String toJSON(Object object) {
        return JSON.toJSONString(object,config);
    }

    public static <T> T parseObject(String json,Class<T> cls){
        return JSON.parseObject(json, cls);
    }

    public static <T> List<T> parseArray(String json,Class<T> cls){
        return JSON.parseArray(json, cls);
    }


    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("time",new Timestamp(System.currentTimeMillis()));
        System.out.printf(JsonUtils.toJSON(map));

//        Object object = JsonUtils.parseObject("{\"time\":\"2015-04-07 16:48:08\"}",Test.class);
//        System.out.printf(object.toString());


    }
}


